package com.changgou.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.config.TokenDecode;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kfzhao
 * @version 1.0
 * @className SeckillOrderController
 * @description
 * @date 2022/1/6 15:12
 */
@RestController
@RequestMapping("/seckillorder")
public class SeckillOrderController {
    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillOrderService seckillOrderService;

    /**
     * 秒杀下单
     *
     * @param time 时间段
     * @param id   商品id
     * @return com.changgou.entity.Result
     * @throws
     * @description
     * @author kfzhao
     * @date 2022/1/6 15:13
     */
    @RequestMapping("/add")
    public Result add(@RequestParam("time") String time, @RequestParam("id") Long id) {

        // 1.动态获取当前的登录人
        String username = tokenDecode.getUserInfo().get("username");

        // 2.基于业务层进行秒杀下单
        boolean result = seckillOrderService.add(time, id, username);
        // 3.返回结果
        if (result) {
            // 下单成功
            return new Result(true, StatusCode.OK, "秒杀下单成功");
        } else {
            // 下单失败
            return new Result(false, StatusCode.ERROR, "秒杀下单失败");
        }
    }


}
